<?php 
session_start();
include "../_init_.php";

$dt = new DataTable(constant('DB_TABLE_EQUIPMENT'));
//增加数据
$action= isset($_REQUEST["action"])?$_REQUEST["action"]:'';
switch($action)
{
	case 'edit':
		$info = $dt->get($_REQUEST['id']);
	break;
	case 'save':
		$data = $_REQUEST;
		try
		{
			$is_new = true;
			$data = checkData($data);
			if(isset($data['id']) && intval($data['id']) > 0)
				$is_new = false;
			$dt->save($data);
			if($is_new)
				$sucess_message = '新增家電選項成功';
			else
				$sucess_message = '修改家電選項成功';
		}catch(Exception $ex)
		{
			$action = 'edit';
			$info = $data;
			$error_message = $ex->getMessage();
		}
	break;
	case 'delete':
		try
		{
			$dt->remove($_REQUEST['id']);
			$sucess_message = '刪除家電選項成功';
		}
		catch(Exception $ex)
		{
			$error_message = $ex->getMessage();
		}
	break;
	case 'new':
		$info = array();
	break;
}

function checkData($data)
{
	global $dt;
	if(strlen($data['name']) == 0)
		throw new Exception("請输入名稱!");
	$condition = sprintf("`name`='%s' AND `id` !=%d",$data['name'],$data['id']);
	$row = $dt->get_row($condition);
	if($row)
	{
		throw new Exception($data['name'].' 已有的[家電選項]！');
	}

	return $data;
}


?>



<html>
<head>

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>網站管理系統</title>


<LINK REL="stylesheet" HREF="css/style.css" TYPE="text/css">
<LINK REL="stylesheet" HREF="css/main.css" TYPE="text/css">
<script src="js/FancyZoom.js" language="JavaScript" type="text/javascript"></script>
<script src="js/FancyZoomHTML.js" language="JavaScript" type="text/javascript"></script>
<body onLoad="setupZoom();">

<script language="javascript">
<!--

function ConfirmDel()
{
   if(confirm("確定要刪除選擇的項目嗎？一旦刪除將不能恢複！"))
     return true;
   else
     return false;	 
}


</SCRIPT>

<script language="javascript">
	function check(form1){
		if(form1.name.value==""){
			alert("請输入名稱!");form1.name.focus();return false;		
		}
	}
</script>

<table width="98%" border="0" class="table2">
  <tr>
    <td>房源管理 - 家電管理</td>
  </tr>
</table>
<?php if($error_message):?>
<div class="error_message"><?php echo $error_message;?></div>
<?php endif;?>
<?php if($sucess_message):?>
<div class="success_message"><?php echo $sucess_message;?></div>
<?php endif;?>
<table width="100%" height="100%" border="0" cellpadding="0" cellspacing="0">
  <tr> 
    <td align="center" valign="top">
      </strong> 
      <?php if($action == 'new' || $action == 'edit'):?>
      <table width="820" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr> 
          <td width="851"  height="20" class="back_southidc" > <div align="center"><strong>家電選項</strong></div></td>
        </tr>
        <tr class="tr_southidc"> 
          <FORM   method="post" name="form1"  onsubmit="return check(this);">	
          <input type="hidden" name="action" value="save"/>
          <input type="hidden" name="id" value="<?php echo $info['id'];?>"/>
            <td bgcolor="#B0DAFF"><table width="100%" border="0" align="center" cellspacing="1" >
                
                <tr> 
                  <td width="13%" height="22" bgcolor="#FFFFFF"> <div align="right">名稱：</div></td>
                  <td width="87%" bgcolor="#FFFFFF"><input name="name" type="text" id="login_user" size="15" maxlength="20" value="<?php echo $info['name'];?>"></td>
                </tr>			
				
                <tr> 
                  <td height="22" bgcolor="#FFFFFF"> <div align="right">排序：</div></td>
                  <td bgcolor="#FFFFFF"><input name="sn" type="text" size="16" maxlength="20" value="<?php echo $info['sn'];?>"></td>
                </tr>
				
                <tr> 
                  <td height="22" colspan="2" bgcolor="#FFFFFF"><div align="center">
                      
                      <INPUT type=submit    name=Submit2 value="確認送出">
                  </div></td>
                </tr>
            </table></td>
          </form>
        </tr>
      </table>
      <br> 
      <?php endif;?>
      <table width="820" border="0" cellpadding="2" cellspacing="1" class="table_southidc">
        <tr  class="tr_southidc">          
            <td bgcolor="#B0DAFF">
              <table width="100%" border="0" align="center" cellpadding="0" cellspacing="2" bordercolor="#0000FF">
                <tr bgcolor="#E4F2FF"> 
                  <td width="10%" height="25"  bgcolor="#D9EDFF"> 
                  <div align="center">名稱</div></td>
                  <td width="10%" bgcolor="#D9EDFF"> 
                  <div align="center">排序</div></td>
                  <td width="8%" bgcolor="#D9EDFF"> 
                  <div align="center">操作</div></td>
                  <td width="5%" bgcolor="#D9EDFF"> 
                  <div align="center">操作</div></td>
                </tr>
<?php 
	$admin_list = $dt->get_list('','sn');
	
	foreach($admin_list as $row):
	?>
 <tr bgcolor="#FFFFFF" onMouseOver="this.style.backgroundColor='#E4F2FF'" onMouseOut="this.style.backgroundColor=''"> 
                  <td height="22"> 
                  <div align="center"><?php echo $row['name'];?></div></td>
                  <td> 
                  <div align="center"><?php echo $row['sn'];?></div></td>
                  <td> 
                    <div align="center">
                  <a href='?action=edit&id=<?php echo $row['id'];?>'>修改</a>                    </div></td>
                  <td> 
                    <div align="center">
				  <a href="?id=<?php echo $row['id'];?>&action=delete" onClick="return ConfirmDel();">刪除</a>                    </div></td>
                </tr>
<?php endforeach;?>
          </table></td>         
        </tr>
      </table>
    </td>
  </tr>
</table>
<?php  include("foot.php");?>